package org.lmind.example.uc.biz;

import java.util.Date;
import java.util.Map;
import java.util.Map.Entry;

import javax.annotation.Resource;
import javax.ejb.TransactionAttribute;
import javax.persistence.NoResultException;

import org.lmind.example.common.core.jpa.EntityManagerHelper;
import org.lmind.example.common.entity.User;
import org.lmind.example.uc.api.LoginFailException;
import org.lmind.example.uc.api.UserCenter;
import org.lmind.example.uc.api.UserCommonAttrubtes;
import org.lmind.example.uc.api.UserInfo;

public class UserCenterImpl implements UserCenter {
	
	@Resource
	private EntityManagerHelper dao;

	@Override
	@TransactionAttribute
	public UserInfo register(String name, String password) {
		
		User user = new User();
		user.setName(name);
		user.setPassword(password);
		dao.getEntityManager().persist(user);
		dao.getEntityManager().flush();
		
		return new UserInfoImpl(user);
	}

	@Override
	public void changePassword(String name, String password, String newPassword) {
		try {
			String jpql = "select u from User u where u.name=?1 and u.password=?2";
			User user = (User)dao.getSingleResult(jpql, name, password);
			user.setPassword(newPassword);
			dao.getEntityManager().persist(user);
		} catch (NoResultException e) {
			throw new LoginFailException(e);
		}
	}

	@Override
	public void resetPassword(String name, String email) {
		// TODO Auto-generated method stub
		
	}
	
	@Override
	@TransactionAttribute
	public void modifyUserInfo(String name, String password,
			Map<String, Object> attributes) {

		User user = (User) dao.getEntityManager().find(
				User.class, name);
		if (attributes != null) {
			setUserAttributes(user, attributes);
		}
		dao.getEntityManager().persist(user);
		dao.getEntityManager().flush();

	}

	@Override
	@TransactionAttribute
	public UserInfo login(String name, String password) {
		String jpql = "select u from User u where u.name=?1 and u.password=?2";
		try {
			User user = (User)dao.getSingleResult(jpql, name, password);
			UserInfoImpl userinfo = new UserInfoImpl(user);
			return userinfo;
		} catch (NoResultException e) {
			throw new LoginFailException(e);
		}
	}

	@Override
	@TransactionAttribute
	public void changeUserLockStatus(String name, boolean lock) {
		User user = dao.getEntityManager().find(User.class, name);
		user.setLocked(lock);
		dao.getEntityManager().persist(user);
	}
	
	private void setUserAttributes(User user, Map<String, Object> attributes) {

		for (Entry<String, Object> entry : attributes.entrySet()) {
			String name = entry.getKey();
			Object value = entry.getValue();
			if (value == null) {
				throw new IllegalArgumentException("Attributes不可以有空的value");
			}
			if (name.equals(UserCommonAttrubtes.SEX)) {
				user.setSex((Boolean)value);
			} else if (name.equals(UserCommonAttrubtes.BIRTHDAY)) {
				user.setBirthday((Date)value);
			} else if (name.equals(UserCommonAttrubtes.DESCRIPTION)) {
				user.setDescription((String)value);
			}
		}
	}



}
